<template>
  <div class="app-container">
    <!--  统计图标  -->
    <div class="page-top-title">{{ this.name }} 缴费详情</div>
    <div v-show="showCharts">
      <el-row :gutter="32">
        <el-col :xs="24" :sm="24" :lg="12">
          <div class="chart-wrapper">
            <person-chart :finished="this.personChartParams.finished" :un-finished="this.personChartParams.unFinished"/>
          </div>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="12">
          <div class="chart-wrapper">
            <money-chart :paid="this.moneyChartParams.paid" :un-paid="this.moneyChartParams.unPaid"/>
          </div>
        </el-col>
      </el-row>
    </div>

    <!--  统计表格  -->
    <div style="margin-top: 40px">
      <div>
        <el-form :model="queryParams">
          <el-row>
            <el-col :span="7">
              <el-form-item label="房屋名称:">
                <el-input clearable v-model="queryParams.houseName" style="width: 200px" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="户 主:">
                <el-input clearable v-model="queryParams.name" style="width: 200px" />
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-button type="primary" @click="search()">搜索</el-button>
            </el-col>
            <el-col :span="3">
              <el-button type="primary">批量通知</el-button>
            </el-col>
            <el-col :span="4">
              <!--  隐藏图表或刷新  -->
              <div class="top-right-btn">
                <el-tooltip class="item" effect="dark" :content="showCharts ? '隐藏表格' : '显示表格'" placement="top">
                  <el-button circle icon="el-icon-s-data" @click="toggleChart()" />
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="刷新" placement="top">
                  <el-button circle icon="el-icon-refresh" @click="refresh()" />
                </el-tooltip>
              </div>
            </el-col>
          </el-row>
        </el-form>

        <!--    表格    -->
        <el-table
          v-loading="loading"
          :data="families"
          border
          stripe
          highlight-current-row
          fit
          :default-sort="{prop: 'payFlag',order: 'ascending'}"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" width="55" align="center" />
          <el-table-column label="房屋名称" prop="house.name" align="center" />
          <el-table-column label="户 主" prop="house.userNickName" align="center" />
          <el-table-column label="家庭人数" align="center">
            <template slot-scope="scope">
              {{ scope.row.house.num }} 人
            </template>
          </el-table-column>
          <el-table-column label="应付金额" align="center">
            <template slot-scope="scope">
              {{ scope.row.due }} 元
            </template>
          </el-table-column>
          <el-table-column label="是否缴费" align="center" prop="finished" sortable>
            <template slot-scope="scope">
              <el-tag :type=" scope.row.payFlag ? 'primary' : 'danger'">
                {{ scope.row.payFlag ? '已缴费' : '未交费' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="primary" :disabled="scope.row.payFlag">通知</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList()"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { listHisHouse, statistics } from '../../api/pay/hisHouse'
import personChart from './personChart'
import moneyChart from './moneyChart'

export default {
  name: 'Detail',
  components: {
    personChart,
    moneyChart
  },
  data() {
    return {
      hisId: this.$route.query.hisId,
      name: this.$route.query.name,
      // 遮罩层
      loading: false,
      families: [],
      // 控制图表显示或隐藏
      showCharts: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hisId: this.$route.query.hisId,
        houseName: '',
        // 户主名称
        name: ''
      },
      // 查询结果条数
      total: 0,
      personChartParams: {
        finished: '',
        unFinished: ''
      },
      moneyChartParams: {
        paid: '',
        unPaid: ''
      }
    }
  },
  mounted() {
    this.getList()
    this.getStatistics()
  },
  methods: {
    getList() {
      this.loading = true
      console.log(this.queryParams)
      listHisHouse(this.queryParams).then(res => {
        this.total = res.total
        this.families = res.rows
        this.loading = false
        console.log(res)
      })
    },
    getStatistics() {
      statistics(this.hisId).then(res => {
        console.log(res)
        res.data.forEach(item => {
          if (item.payFlag) {
            // 已交费信息
            this.personChartParams.finished = item.count.toString()
            this.moneyChartParams.paid = item.sum.toString()
          } else {
            this.personChartParams.unFinished = item.count.toString()
            this.moneyChartParams.unPaid = item.sum.toString()
          }
        })
        console.log(this.personChartParams)
      })
    },
    toggleChart() {
      this.showCharts = !this.showCharts
    },
    search() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
      this.resetQueryParams()
    },
    resetQueryParams() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        hisId: this.$route.query.hisId,
        houseName: '',
        // 户主名称
        name: ''
      }
    }
  }
}
</script>

<style>
  .card-header {
    text-align: center;
    font-weight: bold;
  }
  .card {
    height: 670px;
    margin-left: 5px;
  }
</style>
